package cl.easyRestaurant.ejb;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import cl.easyRestaurant.entity.Ventas;
import java.util.List;
import cl.easyRestaurant.entity.FormaPago;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;





/**
 * Session Bean implementation class pagoCuentasEJB
 */
@Stateless
public class pagoCuentasEJB implements pagoCuentasEJBLocal {

    /**
     * Default constructor. 
     */
    public pagoCuentasEJB() {
        // TODO Auto-generated constructor stub
    }

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
    
    EntityManager em = emf.createEntityManager();
    
    public void crearVenta(Ventas ventas){
    	try{
    		EntityTransaction t = em.getTransaction();
    		t.begin();
    		SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    		Ventas ven = new Ventas();
    		ven.setFechaVenta(df.parse("13-07-2012"));
    		ven.setIdVenta(1);
    		ven.setTotalVenta(10000);
    		em.persist(ven);
    	}catch (Exception e) {
			// TODO: handle exception
    		 System.out.println("Error " + e );
		}
    }
    
    public FormaPago listarFormas(){
    	try{
    		List formaList = em.createQuery(
    				"select forma from formaPago as forma")
    				.getResultList();
    		Iterator formaIt = formaList.iterator();
    		while(formaIt.hasNext()) {
    			FormaPago fpago = (FormaPago) formaIt.next();
    			FormaPago objFPago = new FormaPago();
    			objFPago.setIdForma(fpago.getIdForma());
    			objFPago.setGlosaForma(fpago.getGlosaForma());
    			return objFPago;
    			
    		}
    	}
    	catch (Exception e) {
			// TODO: handle exception
    		System.out.println("Error al realizar consulta" + e);
		}
    	return null;
    	
    }
    
}
